package com.amigo.online.consumer.video.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

import com.amigo.online.common.core.response.Result;
import com.amigo.online.consumer.video.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.amigo.online.common.base.controller.BaseConsumerController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.param.dto.video.CheckVersionDto;
import com.amigo.online.consumer.video.common.ConstantValue;
import com.amigo.online.consumer.video.util.FastDFSClient;
import com.amigo.online.consumer.video.util.PropertiesUtil;

@RestController
@RequestMapping(value = "/app", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class AppVersionController extends BaseConsumerController {
//	@Autowired
//	FastDFSClient fastDFSClient;
	@Autowired
	UserService userService;
	@CrossOrigin
	@PostMapping("/uploadApp")
	public ResponseEntity<Response> uploadApp(@RequestPart(value = "file", required = false) MultipartFile file,
			@RequestParam String version, @RequestParam boolean forceUpdate) throws FileNotFoundException, IOException {
		// 不传文件则是ios
		// TODO: 2019/10/11 重构时需要注意
		if (file == null || file.isEmpty()) {
			PropertiesUtil.writeProperty("ios_" + version + "_" + forceUpdate, "");
			return success(true);
		}
		String filePath = "";
		String extName = "apk";
		String appName = "tevitalk_" + version + "." + extName;
		try {
			filePath = FastDFSClient.uploadFile(file.getBytes(), extName);
			String downloadUrl = "http://" + ConstantValue.FASTDSF_PORT + "/" + filePath + "?attname=" + appName;
			PropertiesUtil.writeProperty("android_" + version + "_" + forceUpdate, downloadUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return success(true);
	}

//	@PostMapping("/checkVersion")
//	public ResponseEntity<Response> checkVersion(@RequestBody CheckVersionDto dto) {
//		Properties properties = PropertiesUtil.getProperties();
//		boolean isLatestVersion = true;
//		// TODO: 2019/10/11 重构时需要注意
//		String downloadUrl = "";
//		boolean isForceUpdate = false;
//		if (properties != null) {
//			String latestVersion = getLatestVersion(properties, dto.getType());
//			// latestVersion = android_1.00 或者ios_1.00
//			try {
//				isLatestVersion = Integer.valueOf(dto.getVersion()) >= Integer.valueOf(latestVersion.split("_")[1]);
//			} catch (NumberFormatException e) {
//				isLatestVersion = false;
//				e.printStackTrace();
//			}
//			downloadUrl = isLatestVersion ? "" : (String) properties.get(latestVersion);
//			isForceUpdate = Objects.equals("true", latestVersion.split("_")[2]);
//		}
//		Map<String, Object> map = new HashMap<>();
//		map.put("isLatestVersion", isLatestVersion);
//		map.put("downloadUrl", downloadUrl);
//		map.put("isForceUpdate", isForceUpdate);
//		return success(map);
//	}
//private String getLatestVersion(Properties properties, Integer type) {
//	// type 1-android 2-ios
//	// TODO: 2019/10/11 重构时需要注意
//	String typeValue = type == 1 ? "android" : "ios";
//	Set<Object> keySet = properties.keySet();
//	String latestVersion = "";
//	for (Object key : keySet) {
//		String keyStr = (String) key;
//		if (keyStr.contains(typeValue)) {
//			// keyStr = android_1.00 或者ios_1.00
//			latestVersion = keyStr;
//			break;
//		}
//	}
//	return latestVersion;
//}
	@PostMapping("/checkVersion")
	public Result checkVersion(@RequestBody CheckVersionDto dto) {
		return userService.checkVersion(dto);
	}

}
